package com.oig.sys.msg.controller;


import com.oig.common.exception.BadRequestException;
import com.oig.common.module.SysResponse;
import com.oig.common.validator.GroupAdd;
import com.oig.common.validator.GroupEdit;
import com.oig.sys.msg.dto.PushChannelDto;
import com.oig.sys.msg.service.ITbPushChannelService;
import com.oig.sys.msg.vo.PushChannelVo;
import com.oig.sys.security.util.SecurityUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Tag(name = "消息渠道管理")
@RestController
@RequestMapping("/pushChannel")
@Slf4j
@RequiredArgsConstructor
public class TbPushChannelController {

    private final ITbPushChannelService pushChannelService;


    /**
     * 消息后面要加推送方式，一个消息可以多种推送方式，  短信，email，站内信，企业微信，微信等等
     */
    @Operation(summary = "添加渠道")
    @PostMapping(value = "/add")
    public PushChannelVo addPushChannel(@RequestBody @Validated(GroupAdd.class) PushChannelDto pushChannelDto) {
        log.info("add push channel, {}", pushChannelDto);
        pushChannelDto.setTenantId(SecurityUtil.getDefineUserMain().getTenantId());        //数据改成 租户公共数据
        pushChannelDto.setTenantName(SecurityUtil.getDefineUserMain().getTenantName());
        pushChannelDto.setCreateUserId(SecurityUtil.getDefineUserMain().getId());
        pushChannelDto.setCreateUserName(SecurityUtil.getDefineUserMain().getRealName());
        pushChannelDto.setCreateTime(new Date());
        return this.pushChannelService.addPushChannel(pushChannelDto);
    }

    private PushChannelVo checkRole(Integer pushChannelId) {
        PushChannelVo pushChannel = this.pushChannelService.getPushChannel(pushChannelId);
        if (pushChannel == null) {
            throw new BadRequestException("数据不存在");
        }
        SecurityUtil.getDefineUserMain().checkTenantPermission(pushChannel.getTenantId());
        return pushChannel;
    }

    @Operation(summary = "修改渠道")
    @PostMapping(value = "/update")
    public PushChannelVo updatePushChannel(@RequestBody @Validated(GroupEdit.class) PushChannelDto pushChannelDto) {
        log.info("update push channel, {}", pushChannelDto);
        this.checkRole(pushChannelDto.getId());
        pushChannelDto.setLastUpdateId(SecurityUtil.getDefineUserMain().getId());
        pushChannelDto.setLastUpdateName(SecurityUtil.getDefineUserMain().getRealName());
        pushChannelDto.setLastUpdateTime(new Date());
        return this.pushChannelService.updatePushChannel(pushChannelDto);
    }


    @Operation(summary = "物理删除渠道")
    @PostMapping(value = "/del/{channelId}")
    public SysResponse<Integer> delPushChannel(@PathVariable("channelId") Integer channelId) {
        log.info("del push channel id={}", channelId);
        this.checkRole(channelId);
        this.pushChannelService.delPushChannel(channelId);
        return SysResponse.ok(channelId, "物理删除成功");
    }

    @Operation(summary = "渠道详细信息")
    @PostMapping(value = "/info/{channelId}")
    public PushChannelVo getPushChannel(@PathVariable("channelId") Integer channelId) {
        log.info(" get push channel id=:{}", channelId);
        return this.checkRole(channelId);
    }



    @Operation(summary = "渠道列表")
    @Parameters({
            @Parameter(name = "supportMsgType", description = "渠道类型"),
            @Parameter(name = "tenantId", description = "--改成租户id")
    })
    @PostMapping(value = "/listChannel/{supportMsgType}/{tenantId}")
    public List<PushChannelVo> listChannel(@PathVariable("supportMsgType") String supportMsgType, @PathVariable("tenantId") Integer tenantId) {
        log.info(" list push channel supper type=:{} tenantId=:{}", supportMsgType, tenantId);
        return this.pushChannelService.listChannel(supportMsgType, tenantId);
    }

}
